package view.component.menu
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Screen;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	import starling.textures.Texture;
	
	import util.AtlasTextureGenerator;
	import util.FG2FUGenerator;
	
	import view.component.IAnimateNaviView;
	
	
	/**
	 * 主菜单页
	 * @author £iang
	 * 
	 */	
	public class MenuView extends Screen implements IAnimateNaviView
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 按钮管理数组 */
		public var buttonVec:Vector.<Button> = new Vector.<Button>;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 按钮宽度 */
		private var _buttonW:Number;
		/** 按钮高度 */
		private var _buttonH:Number;
		
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */	
		override protected function initialize():void
		{
			var nameVec:Vector.<String> = new Vector.<String>;
			
			nameVec.push(
				"怪物情报", "武器情报", "防具情报", 
				"技能情报", "任务情报", "地图情报", 
				"道具情报", "猫猫情报", "村落情报",
				"预留情报", "预留情报", "预留情报"
			);
			
			var iconVec:Vector.<Texture> = AtlasTextureGenerator.getRandomCatTextures(
				"all", 6 );
			
			_buttonW = ( UISize.MAIN_W - 
				UISize.MENU_HOR_PADDING * dpiScale * 2 - 
				UISize.MENU_BTN_INTERVEL * dpiScale ) / 2;
			_buttonH = ( UISize.MAIN_H - 
				UISize.MENU_VER_PADDING * dpiScale * 2 -
				UISize.MENU_BTN_INTERVEL * dpiScale * 5 ) / 6;
			
			var tmpButton:Button;
			
			for ( var i:int = 0; i < 12; i ++ )
			{
				tmpButton = FG2FUGenerator.generateRoundRectButton(
					_buttonW / this.dpiScale, _buttonH / this.dpiScale, this.dpiScale,
					12, "small"
				);
				
				var iconImage:Image = new Image( iconVec[ int( i / 2 ) ] as Texture );
				
				iconImage.scaleX = iconImage.scaleY = this.dpiScale;
				tmpButton.defaultIcon = iconImage;
				tmpButton.label = nameVec[ i ] as String;
				tmpButton.iconPosition = ( i % 2 == 0 ) ? "left" : "right";
				tmpButton.gap = 20 * dpiScale;
				buttonVec.push( tmpButton );
				this.addChild( buttonVec[ i ] );
			}
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			for ( var i:int = 0; i < 12; i ++ )
			{
				if ( i % 2 == 0 )
				{
					( buttonVec[ i ] as Button ).x = _buttonW * ( -1 );
				}
				else
				{
					( buttonVec[ i ] as Button ).x = UISize.MAIN_W;
				}
				
				( buttonVec[ i ] as Button ).y = UISize.MENU_VER_PADDING * dpiScale +
					int( i / 2 ) * ( _buttonH + UISize.MENU_BTN_INTERVEL * dpiScale );
			}
		}
		
		/**
		 * 添加侦听器 
		 * 
		 */		
		private function addListener():void
		{
			( buttonVec[0] as Button ).addEventListener( Event.TRIGGERED, monsterTriggeredHandler );
			( buttonVec[1] as Button ).addEventListener( Event.TRIGGERED, weaponTriggeredHandler );
		}
	
		/**
		 * 播放显示动画
		 * 
		 */	
		public function playShowAnimate():void
		{
			var tmpTween:Tween;
			
			for ( var i:int = 0; i < 12; i ++ )
			{
				var toX:Number;
				
				tmpTween = new Tween( buttonVec[ i ], 0.4, "easeOut" );
				tmpTween.delay =  int( i / 2 ) * 0.1;
				
				if ( i % 2 == 0 )
				{
					toX = UISize.MENU_HOR_PADDING;
				}
				else
				{
					toX = UISize.MENU_HOR_PADDING * dpiScale + 
						_buttonW + UISize.MENU_BTN_INTERVEL * dpiScale;
				}
				
				tmpTween.animate( "x", toX );
				Starling.juggler.add( tmpTween );
			}
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */	
		public function playHideAnimate():void
		{
			var tmpTween:Tween;
			
			for ( var i:int = 0; i < 12; i ++ )
			{
				var toX:Number;
				
				tmpTween = new Tween( buttonVec[ i ], 0.4, "easeOut" );
				tmpTween.delay =  int( i / 2 ) * 0.1;
				
				if ( i % 2 == 0 )
				{
					toX = _buttonW * ( -1 );
				}
				else
				{
					toX = UISize.MAIN_W;
				}
				
				tmpTween.animate( "x", toX );
				Starling.juggler.add( tmpTween );
			}
		}
		
		
		/**
		 * 点击按钮通用函数 
		 * @param eventName 派发的事件名
		 * 
		 */		
		private function buttonTriggered( eventName:String ):void
		{
			this.touchable = false;
			showOtherScreen( eventName );
		}
		
		/**
		 * 显示其他导航页面 
		 * @param eventName
		 * 
		 */		
		private function showOtherScreen( eventName:String ):void
		{
			dispatchEventWith( eventName );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击怪物按钮后派发的事件 
		 * @param event 点击按钮后派发的事件 
		 * 
		 */		
		protected function monsterTriggeredHandler( event:Event ):void
		{
			buttonTriggered( "gotoMonster" );
		}
		
		/**
		 * 处理点击武器按钮后派发的事件 
		 * @param event 点击按钮后派发的事件 
		 * 
		 */		
		protected function weaponTriggeredHandler( event:Event ):void
		{
			buttonTriggered( "gotoWeapon" );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			for ( var i:int = 0; i < 12; i ++ )
			{
				Starling.juggler.removeTweens( buttonVec[ i ] );
				( buttonVec[ i ] as Button ).dispose();
				FG2FUGenerator.deepDisposeButtonComp( ( buttonVec[ i ] as Button ) );
				buttonVec[ i ] = null;	
			}
			
			buttonVec.length = 0;
			buttonVec = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function MenuView()
		{
			super();
		}
	}
}